if(!jsx3.lang.Class.forName("tibco.admin.enterpriseAssets.envDef.envDtls.keystore.Keystore")) {

    jsx3.lang.Package.definePackage("tibco.admin.enterpriseAssets.envDef.envDtls.keystore", function (envKeystorePak) {
    });

    jsx3.lang.Class.defineClass("tibco.admin.enterpriseAssets.envDef.envDtls.keystore.Keystore",
            jsx3.gui.Block,
            null,
            function (envKeystore) {
                envKeystore.prototype.mtxList = null;
                envKeystore.prototype.selectBox = null;
                envKeystore.prototype.environmentName = null
                envKeystore.prototype.entityID = null;

                envKeystore.prototype.subscribeAll = function() {
                    this.getDescendantOfName("btnEdit27G").setEnabled(jsx3.gui.Form.STATEDISABLED, true);
                    this.mtxList = this.getDescendantOfName("mtxKeystoreList27G");
                    this.selectBox = this.getDescendantOfName("selFilterEnable27G");
                    this.selectBox.setEnabled(jsx3.gui.Form.STATEDISABLED, true);
                    tibco.admin.PubSub.subscribe(tibco.admin.enterpriseAssets.envDef.EventID.ENVIRONMENT_ROW_SELECTED, this, this.setDetails);
                }

                envKeystore.prototype.clearAll = function() {
                    this.environmentName = null;
                    this.entityID = null;
                    tibco.admin.Main.getServer().getCache().clearById(this.mtxList.getXMLId());
                    var editButton = this.getDescendantOfName("btnEdit27G");
                    if(this.handler && editButton.getDisplay() == jsx3.gui.Block.DISPLAYNONE) {
                        this.handler.endEditMode();
                        editButton.setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true).
                                getParent().getChild("btnCancel27G").setDisplay(jsx3.gui.Block.DISPLAYNONE, true).
                                getParent().getChild("btnAdd27G").setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
                        this.mtxList.getDescendantOfName("mcAssociated27G").setValueTemplate(tibco.admin.Main.getServer().getDynamicProperty("@@Keystores Enabled View Mode Value Template"));
                        this.mtxList.repaint();
                    }
                    editButton.setEnabled(jsx3.gui.Form.STATEDISABLED, true);
                    this.selectBox.setEnabled(jsx3.gui.Form.STATEDISABLED, true).setValue(tibco.admin.Main.getServer().getDynamicProperty("@@Filter Option All"));
                }

                envKeystore.prototype.unsubscribeAll = function() {
                    tibco.admin.PubSub.unsubscribe(tibco.admin.enterpriseAssets.envDef.EventID.ENVIRONMENT_ROW_SELECTED, this);
                }

                envKeystore.setToggle = function(jsxid, toggleMode) {
                    var record = tibco.admin.Main.getServer().getCache().getDocument("envKeystoreCache27G").selectSingleNode("//record[@jsxid='" + jsxid + "']");
                    if(record) {
                        record.setAttribute("associated", toggleMode);
                    }
                    var tempCacheRecord = tibco.admin.Main.getServer().getCache().getDocument("temp27G").selectSingleNode("//record[@jsxid='" + jsxid + "']");
                    if(tempCacheRecord) {
                        tempCacheRecord.setAttribute("associated", toggleMode);
                    }
                }

                envKeystore.prototype.setDetails = function(objEvent) {
                    if(this.handler && this.handler.isInEditMode()) {
                        return;
                    }
                    if(objEvent.environmentName) {
                        this.selectBox.setValue(tibco.admin.Main.getServer().getDynamicProperty("@@Filter Option All"));
                        this.environmentName = objEvent.environmentName;
                        this.entityID = objEvent.environmentID;
                        this.callgetKeystoresAssociatedWithEnvironment();
                    }
                    else {
                        this.clearAll();
                    }
                }

                envKeystore.prototype.callgetKeystoresAssociatedWithEnvironment = function() {
                    if(this.entityID && (!this.handler || !this.handler.isInEditMode())) {
                        var objService = tibco.admin.Main.loadService("tibco.admin",
                                "tibco/admin/enterpriseAssets/envDef/envDtls/keystore/rules/getKeystoresInEnvironmentRules.xml",
                                "getKeystoresAssociatedWithEnvironment",
                                "Get keystore for environments");
                        //subscribe and call
                        objService.environmentName = this.environmentName;
                        objService.entityID = this.entityID;
                        objService.subscribe(jsx3.net.Service.ON_SUCCESS, this, this.ongetKeystoresAssociatedWithEnvironmentSuccess);
                        objService.setEndpointURL(tibco.admin.util.ServiceHelper.getKeystoreEndpointURL());
                        objService.doCall();
                    }
                };

                envKeystore.prototype.ongetKeystoresAssociatedWithEnvironmentSuccess = function(objEvent) {
                    if(this.mtxList.getXML().selectNodes("//record").getLength() > 0) {
                        this.getDescendantOfName("btnEdit27G").setEnabled(jsx3.gui.Form.STATEENABLED, true);
                        this.selectBox.setEnabled(jsx3.gui.Form.STATEENABLED, true);
                        var tempCacheDoc = this.mtxList.getXML().cloneDocument();
                        tibco.admin.Main.getServer().getCache().setDocument("temp27G", tempCacheDoc);
                        var selectedValue = this.selectBox.getValue();
                        if(selectedValue != tibco.admin.Main.getServer().getDynamicProperty("@@All27G")) {
                            this.filterResults(selectedValue);
                        }
                        else {
                            this.mtxList.repaintData();
                        }
                    }

                };

                envKeystore.prototype.callassociateKeystoresWithEnvironment = function() {
                    var objService = tibco.admin.Main.loadService("tibco.admin",
                            "tibco/admin/enterpriseAssets/envDef/envDtls/keystore/rules/associateEnvToKeystoresRules.xml",
                            "associateKeystoresWithEnvironment",
                            "associate keystores for environments");
                    //subscribe and call
                    objService.environmentName = this.environmentName;
                    objService.entityID = this.entityID;
                    objService.subscribe(jsx3.net.Service.ON_SUCCESS, this, this.onassociateKeystoresWithEnvironmentSuccess);
                    objService.setEndpointURL(tibco.admin.util.ServiceHelper.getKeystoreEndpointURL())
                    objService.doCall();
                };

                envKeystore.prototype.onassociateKeystoresWithEnvironmentSuccess = function(objEvent) {
                    this.callgetKeystoresAssociatedWithEnvironment();
                    this.editSettings();
                };

                envKeystore.prototype.cancelEditSettings = function() {
                    this.editSettings();
                    this.callgetKeystoresAssociatedWithEnvironment();
                }

                envKeystore.prototype.editSettings = function() {
                    var button = this.getDescendantOfName("btnEdit27G");
                    if(button.getDisplay() == jsx3.gui.Block.DISPLAYBLOCK) {
                        this.handler.beginEditMode();
                        button.setDisplay(jsx3.gui.Block.DISPLAYNONE, true).
                                getParent().getChild("btnCancel27G").setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true).
                                getParent().getChild("btnAdd27G").setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);
                        this.mtxList.getDescendantOfName("mcAssociated27G").setValueTemplate(tibco.admin.Main.getServer().getDynamicProperty("@@Keystores Enabled Edit Mode Value Template"));
                        this.mtxList.repaintData();
                    }
                    else {
                        this.handler.endEditMode();
                        button.setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true).
                                getParent().getChild("btnCancel27G").setDisplay(jsx3.gui.Block.DISPLAYNONE, true).
                                getParent().getChild("btnAdd27G").setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
                        this.mtxList.getDescendantOfName("mcAssociated27G").setValueTemplate(tibco.admin.Main.getServer().getDynamicProperty("@@Keystores Enabled View Mode Value Template"));
                        this.mtxList.repaintData();

                    }
                }

                envKeystore.prototype.filterResults = function(associatedAttribute) {
                    var tempCacheDoc = tibco.admin.Main.getServer().getCache().getDocument("temp27G");
                    if(associatedAttribute == tibco.admin.Main.getServer().getDynamicProperty("@@Filter Option All")) {
                        this.mtxList.setXMLTransformers(null);
                        if(!tempCacheDoc)
                            this.callgetKeystoresAssociatedWithEnvironment();
                        else {
                            this.mtxList.setSourceXML(tempCacheDoc);
                        }
                    }
                    else {
                        associatedAttribute =
                        (associatedAttribute == tibco.admin.Main.getServer().getDynamicProperty("@@Filter Option All Enabled"))?
                        "true":"false";
                        var objCacheDoc = tempCacheDoc.cloneDocument();
                        this.mtxList.setXMLTransformers(tibco.admin.Main.resolveURI("tibco.admin", "tibco/admin/enterpriseAssets/envDef/envDtls/keystore/xsl/transform.xsl"));
                        this.mtxList.setXSLParam("filterParam", associatedAttribute);
                        this.mtxList.setSourceXML(objCacheDoc);
                    }
                    this.mtxList.repaintData();
                }
            })
}